<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">

      <template slot="menuLeft">
        <!-- 自定义按钮 -->
        <!--<el-button type="primary" size="small" @click="dialogFormVisible = true">打开Dialog</el-button>-->
      </template>

    </avue-crud>

    <!--<el-dialog title="收货地址" :visible.sync="dialogFormVisible"
               modal-append-to-body="false" append-to-body="true" width="70%"
               :close-on-click-modal="false" :show-close="true" top="11vh"  :lock-scroll="true" :fullscreen="false" v-if="dialogFormVisible"
               destroy-on-close="true">
      <el-form :model="form">
        <el-form-item label="活动名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>-->

  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/lamp/workstrategydata";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 350,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
            addBtn: false,
            addRowBtn: false,
            delBtn: false,
            menuWidth: 120,
          column: [
            {
              label: "设备编码",
              prop: "deviceSn",
              rules: [{
                required: true,
                message: "请输入设备编码",
                trigger: "blur"
              }]
            },
            {
              label: "电池串数",
              prop: "batteryModeSeries",
              rules: [{
                required: true,
                message: "请输入电池型号-电池串数",
                trigger: "blur"
              }]
            },
            {
              label: "电池类型",
              prop: "batteryModeType",
              rules: [{
                required: true,
                message: "请输入电池型号-电池类型",
                trigger: "blur"
              }]
            },
            {
              label: "降功开始电压",
              prop: "reductionBeginVoltage",
                minWidth:120,
              rules: [{
                required: true,
                message: "请输入降功开始电压",
                trigger: "blur"
              }]
            },
            {
              label: "降功结束电压",
              prop: "reductionEndVoltage",
                minWidth:120,
              rules: [{
                required: true,
                message: "请输入降功结束电压",
                trigger: "blur"
              }]
            },
            {
              label: "亮度模式",
              prop: "workModeLight",
              rules: [{
                required: true,
                message: "请输入工作类型-亮度模式",
                trigger: "blur"
              }]
            },
            {
              label: "降功模式",
              prop: "workModeReduce",
              rules: [{
                required: true,
                message: "请输入工作类型-降功模式",
                trigger: "blur"
              }]
            },
            {
              label: "时段T1",
              prop: "t1",
              rules: [{
                required: true,
                message: "请输入时段T1",
                trigger: "blur"
              }]
            },
            {
              label: "时段T2",
              prop: "t2",
              rules: [{
                required: true,
                message: "请输入时段T2",
                trigger: "blur"
              }]
            },
            {
              label: "时段T3",
              prop: "t3",
              rules: [{
                required: true,
                message: "请输入时段T3",
                trigger: "blur"
              }]
            },
            {
              label: "时段T4",
              prop: "t4",
              rules: [{
                required: true,
                message: "请输入时段T4",
                trigger: "blur"
              }]
            },
            {
              label: "时段T5",
              prop: "t5",
              rules: [{
                required: true,
                message: "请输入时段T5",
                trigger: "blur"
              }]
            },
            {
              label: "时段T6",
              prop: "t6",
              rules: [{
                required: true,
                message: "请输入时段T6",
                trigger: "blur"
              }]
            },
            {
              label: "时段T7",
              prop: "t7",
              rules: [{
                required: true,
                message: "请输入时段T7",
                trigger: "blur"
              }]
            },
            {
              label: "充电返回电压",
              prop: "chargeReturnVoltage",
                minWidth:120,
              rules: [{
                required: true,
                message: "请输入充电返回电压",
                trigger: "blur"
              }]
            },
            {
              label: "满电电压",
              prop: "maxVoltage",
              rules: [{
                required: true,
                message: "请输入满电电压",
                trigger: "blur"
              }]
            },
            {
              label: "光控电压",
              prop: "lightControlVoltage",
              rules: [{
                required: true,
                message: "请输入光控电压",
                trigger: "blur"
              }]
            },
            {
              label: "光控延时",
              prop: "lightControlDelay",
              rules: [{
                required: true,
                message: "请输入光控延时",
                trigger: "blur"
              }]
            },
            {
              label: "提升充电压",
              prop: "raiseVoltage",
                minWidth:110,
              rules: [{
                required: true,
                message: "请输入提升充电压",
                trigger: "blur"
              }]
            },
            {
              label: "超压保护",
              prop: "overVoltageProtect",
              rules: [{
                required: true,
                message: "请输入超压保护",
                trigger: "blur"
              }]
            },
            {
              label: "充电限制电压",
              prop: "limitVoltage",
                minWidth:120,
              rules: [{
                required: true,
                message: "请输入充电限制电压",
                trigger: "blur"
              }]
            },
            {
              label: "直充电压",
              prop: "directVoltage",
              rules: [{
                required: true,
                message: "请输入直充电压",
                trigger: "blur"
              }]
            },
            {
              label: "浮充电压",
              prop: "floatVoltage",
              rules: [{
                required: true,
                message: "请输入浮充电压",
                trigger: "blur"
              }]
            },
            {
              label: "过放返回电压",
              prop: "dischargeReturnVoltage",
                minWidth:120,
              rules: [{
                required: true,
                message: "请输入过放返回电压",
                trigger: "blur"
              }]
            },
            {
              label: "过放电压",
              prop: "dischargeVoltage",
              rules: [{
                required: true,
                message: "请输入过放电压",
                trigger: "blur"
              }]
            },
            {
              label: "最低温度",
              prop: "minTemperature",
              rules: [{
                required: true,
                message: "请输入最低温度",
                trigger: "blur"
              }]
            },
            {
              label: "最高温度",
              prop: "maxTemperature",
              rules: [{
                required: true,
                message: "请输入最高温度",
                trigger: "blur"
              }]
            },
            {
              label: "晨亮",
              prop: "morningBright",
              rules: [{
                required: true,
                message: "请输入晨亮",
                trigger: "blur"
              }]
            },
            {
              label: "非时段感应亮度",
              prop: "nonTimeReactionBright",
                minWidth:130,
              rules: [{
                required: true,
                message: "请输入非时段感应值-亮度",
                trigger: "blur"
              }]
            },
            {
              label: "非时段感应开关",
              prop: "nonTimeReactionOnOff",
                minWidth:130,
              rules: [{
                required: true,
                message: "请输入非时段感应值-开关",
                trigger: "blur"
              }]
            },
            {
              label: "非时段亮度值",
              prop: "nonTimeBright",
                minWidth:120,
              rules: [{
                required: true,
                message: "请输入非时段亮度值",
                trigger: "blur"
              }]
            },
            {
              label: "修改时间",
              prop: "updateTime",
              rules: [{
                required: true,
                message: "请输入修改时间",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.workstrategydata_add, false),
          viewBtn: this.vaildData(this.permission.workstrategydata_view, false),
          delBtn: this.vaildData(this.permission.workstrategydata_delete, false),
          editBtn: this.vaildData(this.permission.workstrategydata_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, loading, done) {
        add(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowUpdate(row, index, loading, done) {
        update(row).then(() => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
